package com.ruoyi.ssh.service.impl;

import java.util.List;

import com.ruoyi.fk.domain.Question;
import com.ruoyi.fk.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ssh.mapper.QuestionOptionMapper;
import com.ruoyi.ssh.domain.QuestionOption;
import com.ruoyi.ssh.service.IQuestionOptionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 题目选项Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@Service
public class QuestionOptionServiceImpl implements IQuestionOptionService {
    @Autowired
    private QuestionOptionMapper questionOptionMapper;
    @Autowired
    private QuestionMapper questionMapper;


    /*根据id查询题目选项*/
    @Override
    public QuestionOption selectQuestionOptionById(Integer id) {
        return questionOptionMapper.selectQuestionOptionById(id);
    }

    /**
     * 根据题目ID查询选项
     *
     * @param questionId 题目ID
     * @return 题目选项
     */
    @Override
    public QuestionOption selectQuestionOptionByQuestionId(Integer questionId) {
        //先判断题目是否被删除
        Question question = questionMapper.selectQuestionById(questionId);
        if (question.getDel() == 0) {
            throw new RuntimeException("该题目已被删除，不能查询选项");
        } else {
            return questionOptionMapper.selectQuestionOptionByQuestionId(questionId);
        }

    }


    /**
     * 动态添加选项
     *
     * @param questionOption 题目选项
     * @return 结果
     */
    @Override
    @Transactional
    public int insertQuestionOption(QuestionOption questionOption) {
        //判断父类的del是否为0
        Question question = questionMapper.selectQuestionById(questionOption.getQuestionId());
        if (question.getDel() == 0) {
            throw new RuntimeException("该题目已被删除，不能添加选项");
        } else {
            return questionOptionMapper.insertQuestionOption(questionOption);
        }

    }


    /**
     * 动态修改选项
     *
     * @param questionOption 题目选项
     * @return 结果
     */
    @Override
    public int updateQuestionOption(QuestionOption questionOption) {
        return questionOptionMapper.updateQuestionOption(questionOption);
    }


    /*通过题目ID，联表查询出题目和选项全部信息*/
    @Override
    public QuestionOption selectQuestionOptionWithQuestion(Integer questionId) {
        return questionOptionMapper.selectQuestionOptionWithQuestion(questionId);
    }
}
